package cn.libinal.scloud.base.entity;

import cn.libinal.scloud.base.common.entity.JpaBaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.Sets;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.util.Set;

/**
 * @author libinal
 * created at 2018/11/4 15:27
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "sys_menu")
public class Menu extends JpaBaseEntity {

    private static final long serialVersionUID = 2972985719165653177L;
    /**
     * 菜单名
     */
    @Column(name = "menu_name", length = 50, nullable = false)
    private String name;

    /**
     * 菜单编码
     */
    @Column(name = "menu_sn", length = 50, nullable = false, unique = true)
    private String sn;

    /**
     * 菜单URL
     */
    @Column(name = "menu_url", length = 200)
    private String url;

    /**
     * 菜单图标
     */
    @Column(name = "menu_icon", length = 50)
    private String icon;

    /**
     * 优先级
     */
    @Column(name = "priority")
    private Integer priority;

    /**
     * 描述
     */
    @Column(name = "description", length = 200)
    private String description;

    /**
     * 父菜单
     */
    @ManyToOne(targetEntity = Menu.class, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @JsonIgnore
    @JsonIgnoreProperties({"parent", "children"})
    private Menu parent;
    
    /*@OneToMany(mappedBy = "parent", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JsonIgnore
    @JsonIgnoreProperties({"parent", "children"})
    @OrderBy("priority")
    private List<Menu> children = Lists.newArrayList();*/

    /**
     * 菜单权限（默认  INDEX: 显示菜单，VIEW: 查看，CREATE: 创建，UPDATE: 更新，DELETE: 删除）
     */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "menu_id", referencedColumnName = "id")
    private Set<Permission> permissions = Sets.newLinkedHashSet();
}
